--
-- Author: juay
-- Date: 2018-04-10 10:54:27
local M = classSprite("armatureBase")
local DragonBone = import("bbframework.client.si.DragonBone")

function M:ctor(params)
    -- 超类调用
    M.super.ctor(self, params)
    self.file             = params.file            -- 骨骼文件
    self.armatureName    = params.armatureName     -- 骨骼名称
    self.animName        = params.animName         -- 初始骨骼动作名称（选填）
    self.main            = params.main             -- 主场景层
end

-- 节点渲染
function M:onRender()
    -- 超类调用    
    M.super.onRender(self)
    -- 加载骨骼文件
    self:loadFile()
    -- 创建骨骼
    self:createArmature()
    -- 初始化自身尺寸
    self:initSize()
end

-- 加载骨骼文件
function M:loadFile()
    if self.file then      
        DragonBone.addFile(self.file)
    end
end

-- 创建骨骼
function M:createArmature()
    local armature
    if self.animName then
        armature    = DragonBone.createArmatureAndPlay(self.armatureName, self.animName)
    else
        armature    = DragonBone.createArmature(self.armatureName)
    end
    armature:anchor(cc.p(0.5, 0.5))
    local size = armature:getContentSize()
    local anchor = armature:getAnchorPoint()
    
    armature:to(self):p(size.width * anchor.x, size.height * anchor.y)
    -- 添加监听事件
    armature.getAnimation().setMovementEventCallFunc(armature.getAnimation(), handler(self, self.movementHandler))
    self.armature = armature
end

-- 初始化自身尺寸
function M:initSize()
    local size = self.armature:getContentSize()
    self:setContentSize(size)
end

-- 用外部图片换装
-- 默认拍照图片，没拍照则用changeimg指定照片
function M:changeBoneWithFile(boneName, ImgName)
    print("imgPath=======", ImgName)
    print("IO.exists(ImgName)=======", IO.exists(ImgName))
    if IO.exists(ImgName) then
        local bone = self.armature:getBone(boneName)
        bone:changeWithFile(ImgName)
        -- 存在图片，清空缓存
        R.removeSpriteFrameByImageName(ImgName)
    end
end


-- 获取动作的时间
-- return: delayTime  number 时间
function M:getAnimationTime(animationName)
    local delayTime = self.armature:getAnimation():getDuration(animationName) / 24
    return delayTime
end


-- 播放动作
-- param: animName string 动作名称
-- param: fps      number 动作速度
-- param：animOption  table 其他参数
-- return: time number   时间
function M:play(animName , fps, animOption)
    self.armature:play(animName, fps, animOption)
    return self:getAnimationTime(animName)
end

-- 显示骨骼
function M:showBone(boneName, isShowChildren)
    local bone = self.armature:getBone(boneName)
    if bone then
        bone:show(isShowChildren)
    end
end

-- 隐藏骨骼
function M:hideBone(boneName, isShowChildren)
    local bone = self.armature:getBone(boneName)
    if bone then
        bone:hide(isShowChildren)
    end
end

-- 创建新骨头
--[[
    parentBone:         父节点骨头[必选]
    boneImgPath:        新骨头图片路径
    boneName:           新骨头名字
    order  :            层级
]]
function M:createBone(params)
    local newBone = ifnil(params.boneName, "newBone")
    local order = ifnil(params.order, 99)
    print(newBone)
    local bone = ccs.Bone:create(newBone)
    if params.boneImgPath then
        local img = D.img(params.boneImgPath)
        bone:addDisplay(img, 0)     -- 添加一个显示的内容
    end

    bone:changeDisplayWithIndex(0, true)
    bone:setIgnoreMovementBoneData(true)
    self.armature:addBone(bone, params.parentBone)
    bone:zorder(order)
end

-- 设置骨头位置
-- boneName:    骨头名字
-- pos:         坐标
function M:setBonePosition(boneName, pos)
    local bone = self.armature:getBone(boneName)
    if bone then
       bone:setPosition(pos) 
    end
end

-- 设置骨头颜色  cc.c3b(r, g, b)
-- boneName: 骨头名字
-- color:   颜色
function M:setBoneColor(boneName, color)
    local bone = self.armature:getBone(boneName)
    if bone then
        bone:getDisplayRenderNode():setColor(color)
    end
end

-- 获取骨头颜色   
-- boneName: 骨头名字 
function M:getBoneColor(boneName)
    local bone = self.armature:getBone(boneName)
    local color
    if bone then
        color = bone:getDisplayRenderNode():getColor()
    end
    return color
end

-- 骨头颜色渐变
--[[
    boneName:       骨头名称[必选]
    endColor:       目标颜色[必选]
    time:           过程总时间, 默认0.5秒
    changeTime:     渐变单次时间, 默认1/60
]]
function M:changeBoneColor(params)
    local bone = self.armature:getBone(params.boneName)
    local changeTime = ifnil(params.changeTime, 1/60)
    local time = ifnil(params.time, 0.5)
    if bone then
        -- 初始颜色值
        local startColor = self:getBoneColor(params.boneName)
        local changeColor = startColor
        -- 计算每次渐变差值
        local differenceR = (params.endColor["r"] - startColor["r"]) / (time / changeTime)
        local differenceG = (params.endColor["g"] - startColor["g"]) / (time / changeTime)
        local differenceB = (params.endColor["b"] - startColor["b"]) / (time / changeTime)
        self:line({
            A.cycle({
                {"fn", function()
                    -- 颜色值渐变
                    changeColor = cc.c3b(changeColor["r"] + differenceR, changeColor["g"] + differenceG, changeColor["b"] + differenceB)
                    self:setBoneColor(params.boneName, changeColor) 
                end},
                {"delay", changeTime},
            }, time / changeTime),
            {"fn", function()
                -- 计算渐变差值时会有小数点偏差，导致最终值有偏差，强制设置颜色
                self:setBoneColor(params.boneName, params.endColor) 
            end},
        })
    end    
end

-- 设置骨头透明度
-- 有设置颜色需要加getDisplayRenderNode()对皮肤操作，否则只对骨头操作 颜色一开始就会复原
--[[
    boneName:   骨头名
    opacityNum: 透明度
]]
function M:setBoneOpacity(boneName, opacityNum)
    local bone = self.armature:getBone(boneName)
    if bone then
        bone:getDisplayRenderNode():setOpacity(opacityNum)
    end
end

-- 获取骨头透明度
--[[
    boneName:   骨头名
]]
function M:getBoneOpacity(boneName)
    local bone = self.armature:getBone(boneName)
    local opacity
    if bone then
        opacity = bone:getDisplayRenderNode():getOpacity()
    end
    return opacity
end

-- 骨头透明度渐变
--[[
    boneName:       骨头名称[必选]
    endOpacity:     目标透明度[必选]
    time:           过程总时间, 默认0.5秒
    changeTime:     渐变单次时间, 默认1/60
]]
function M:changeBoneOpacity(params)
    local bone = self.armature:getBone(params.boneName)
    local changeTime = ifnil(params.changeTime, 1/60)
    local time = ifnil(params.time, 0.5)
    if bone then
        -- 初始颜色值
        local startOpacity = self:getBoneOpacity(params.boneName)
        local changeOpacity = startOpacity
        -- 计算每次渐变差值
        local differenceOpacity = (params.endOpacity - startOpacity) / (time / changeTime)
        self:line({
            A.cycle({
                {"fn", function()
                    -- 颜色值渐变
                    changeOpacity = changeOpacity + differenceOpacity
                    self:setBoneOpacity(params.boneName, changeOpacity) 
                end},
                {"delay", changeTime},
            }, time / changeTime),
            {"fn", function()
                -- 计算渐变差值时会有小数点偏差，导致最终值有偏差，强制设置最终值
                self:setBoneOpacity(params.boneName, params.endOpacity) 
            end},
        })
    end    
end









-----------------------------------
-- 骨骼监听
-----------------------------------
-- 监听
function M:movementHandler(armature, movementType, movementID)
    
end

function M:onDestructor()
    M.super.onDestructor(self)
end

return M


